package com.clas.liu.myapplication.view.fragment.impl;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.clas.liu.myapplication.R;
import com.clas.liu.myapplication.model.entity.banner.BannerData;
import com.clas.liu.myapplication.model.entity.notice.Notice;
import com.clas.liu.myapplication.model.event.HomeFragmentEvent;
import com.clas.liu.myapplication.presenter.impl.BaseFragmentPresenter;
import com.clas.liu.myapplication.presenter.impl.HomeFragmentPresenter;
import com.clas.liu.myapplication.view.activity.impl.MainActivity;
import com.clas.liu.myapplication.view.activity.impl.NoticeActivity;
import com.clas.liu.myapplication.view.activity.impl.SearchActivity;
import com.clas.liu.myapplication.view.adapter.BannerViewPagerAdapter;
import com.clas.liu.myapplication.view.adapter.HomeCategoryAdapter;
import com.clas.liu.myapplication.view.fragment.i.IHomeFragment;
import com.clas.liu.myapplication.view.util.NetworkHelper;
import com.clas.liu.myapplication.view.util.SaveUtil;
import com.clas.liu.myapplication.view.widget.BannerViewPager;
import com.clas.liu.myapplication.view.widget.MainTabButton;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by liu on 2019/2/28.
 */

public class HomeFragment extends BaseFragment<HomeFragmentPresenter> implements IHomeFragment,View.OnClickListener {


    private BannerViewPager viewPager;
    private BannerViewPagerAdapter bannerdapter;
    private RecyclerView recyclerView;
    private HomeCategoryAdapter adapter;
    private RelativeLayout searchView;
    private MainTabButton warningButton;
    private Context context;
    private View root;

    private List<List<String>> headList;
    private List<BannerData> banners;
    private Handler handler;
    private boolean isFirst = true;

    private int noticeCount;
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = getActivity();
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        root = inflater.inflate(R.layout.fragemnt_home_layout, container, false);
        findView();
        init();
        return root;
    }

    private void init() {
        //测试
        handler = new Handler();
        headList = new ArrayList<>();
        updateHeadAdapter();
        initBanner(banners);
        setListener();
        recyclerView.setNestedScrollingEnabled(false);
    }

    private void setListener() {
        searchView.setOnClickListener(this);
        warningButton.setOnClickListener(this);
    }

    @Override
    public void onResume() {
        super.onResume();
        showNoticeCount();
        if (isFirst) {
            mPresenter.getBanner();
            isFirst = false;
        }
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser) {
            if (viewPager != null && !viewPager.isRunning())
                viewPager.startBanner();
        } else {
            if (viewPager != null && viewPager.isRunning())
                viewPager.stopBanner();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        context = null;
    }

    private void findView() {
        viewPager = root.findViewById(R.id.home_banner_vp);
        recyclerView = root.findViewById(R.id.home_rv);
        searchView = root.findViewById(R.id.home_top_search);
        warningButton = root.findViewById(R.id.home_top_warning_layout);
    }

    public void initBanner(List<BannerData> banners) {
        bannerdapter = new BannerViewPagerAdapter(context, banners);
        viewPager.setAdapter(bannerdapter);
//        viewPager.setCurrentItem(Integer.MAX_VALUE / 2, false);
        if (!viewPager.isRunning())
            viewPager.startBanner();
    }

    @Override
    protected HomeFragmentPresenter getPresenter() {
        return new HomeFragmentPresenter(this);
    }

    @Override
    public void FailBecauseNetReturn(int code) {
            showToast(code + "请求数据失败!");
    }

    @Override
    public void bannerReturn(int code, final List<BannerData> banners) {
        if (code == 0) {
            initBanner(banners);
        }
        mPresenter.getHeads();
    }

    @Override
    public void headsReturn(int code,List<List<String>> heads) {
        if (code != 0) {
            showToast("请求头像异常！");
        }else {
            headList.clear();
            headList.addAll(heads);
            updateHeadAdapter();
            mPresenter.getNoticeCount();
        }
    }

    @Override
    public void countReturn(int count) {
        ((MainActivity)context).setNoticeCount(count);
        noticeCount = count;
        showNoticeCount();
    }

    private void showNoticeCount() {
        int cur_count = SaveUtil.getNoticeCount(context.getApplicationContext());
        if (cur_count<=noticeCount) {
            warningButton.showCount(noticeCount-cur_count);
        }
    }


    private void updateHeadAdapter() {
        if (adapter == null) {
            adapter = new HomeCategoryAdapter(context,headList);
            LinearLayoutManager manager = new LinearLayoutManager(context);
            recyclerView.setLayoutManager(manager);
            recyclerView.setAdapter(adapter);
        }else {
            adapter.updateData(headList);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.home_top_search :
                Intent intent = new Intent(context, SearchActivity.class);
                context.startActivity(intent);
                break;
            case R.id.home_top_warning_layout:
                Intent intent1 = new Intent(context, NoticeActivity.class);
                context.startActivity(intent1);
                break;
        }
    }
}
